package com.example.village.service.impl;

import com.example.village.dao.NeighbourhoodDao;
import com.example.village.dao.UserDao;
import com.example.village.entity.NeighbourhoodEntity;
import com.example.village.entity.UserEntity;
import com.example.village.entityVO.UserDimVO;
import com.example.village.entityVO.UserRegisterVO;
import com.example.village.entityVO.UserVO;
import com.example.village.service.NeighbourhoodService;
import com.example.village.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private NeighbourhoodDao neighbourhoodDao;

    @Override
    public UserEntity login(String username, String password) {
        return userDao.login(username, password);
    }

    @Override
    public Boolean register(UserRegisterVO vo) {
        UserEntity user = new UserEntity();
        user.setUsername(vo.getUsername());
        Integer count = userDao.queryCount(user);
        NeighbourhoodEntity neighbourhood = new NeighbourhoodEntity();
        neighbourhood.setId(vo.getnId());
        neighbourhood = neighbourhoodDao.selectByParam(neighbourhood).get(0);
        if (!neighbourhood.getKey().equals(vo.getKey()) || count > 0){
            return false;
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(vo.getUsername());
        List<UserEntity> users = userDao.queryByParam(userEntity);
        if (users.size() > 0) {
            return false;
        }
        return userDao.register(userEntity) > 0;
    }

    @Override
    public Boolean add(UserEntity user) {
        if (userDao.register(user) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<UserEntity> queryByDim(UserDimVO userDimVO) {
        return userDao.queryByDim(userDimVO);
    }

    @Override
    public List<UserEntity> queryByParam(UserEntity userEntity) {
        return userDao.queryByParam(userEntity);
    }

    @Override
    public Boolean update(UserEntity userEntity) {
        return userDao.update(userEntity) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return userDao.delete(id) > 0;
    }

    @Override
    public Boolean deleteMore(List<Integer> ids) {
        return userDao.deleteMore(ids) > 0;
    }

    @Override
    public List<UserVO> querySelect() {
        return userDao.querySelect();
    }

    @Override
    public UserEntity queryById(Integer id) {
        return userDao.queryById(id);
    }
}
